package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.fc.entity.Room;
import cn.lg.soar.boot.fc.entity.bo.LeaseBo;
import cn.lg.soar.boot.fc.entity.vo.LeaseEasyVo;
import cn.lg.soar.boot.fc.entity.vo.LeaseVo;
import cn.lg.soar.boot.fc.service.RoomService;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.ParameterUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lg.soar.boot.fc.mapper.LeaseMapper;
import cn.lg.soar.boot.fc.entity.Lease;
import cn.lg.soar.boot.fc.service.LeaseService;

import java.io.Serializable;
import java.util.Objects;

/**
 * 合租房间表
 * @author ZhuanZ（无密码）
 * @since 2025-01-18
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class LeaseServiceImpl extends ServiceImpl<LeaseMapper, Lease> implements LeaseService {

    @Autowired
    private RoomService roomService;

    @Override
    public HttpResult<Serializable> create(LeaseBo leaseBo) {
        ParameterUtil.notNull(leaseBo, "数据不能为空");

        Long roomId = leaseBo.getRoomId();

        Room room = roomService.getData(roomId);
        Lease lease = new Lease();
        BeanUtils.copyProperties(leaseBo, lease);
        lease.setCampusId(room.getCampusId());
        lease.setBuildingId(room.getBuildingId());
        lease.setUnitId(room.getUnitId());
        lease.setFloorId(room.getFloorId());

        AssertUtil.isTrue(this.save(lease), "创建失败");
        return HttpResult.<Serializable>success("创建成功").setData("success");
    }

    @Override
    public Lease getData(Long id) {

        if (Objects.isNull(id)) {
            return new Lease();
        }
        return this.getOne(Wrappers.<Lease>lambdaQuery().eq(Lease::getId, id));
    }

    @Override
    public LeaseVo getVoData(Long id) {

        LeaseVo leaseVo = new LeaseVo();

        if (Objects.isNull(id)) {
            return leaseVo;
        }

        Lease lease = this.getData(id);
        BeanUtils.copyProperties(lease, leaseVo);

        return leaseVo;
    }

    @Override
    public LeaseEasyVo getEasyVoData(Long id) {

        LeaseEasyVo leaseEasyVo = new LeaseEasyVo();

        if (Objects.isNull(id)) {
            return leaseEasyVo;
        }

        Lease lease = this.getData(id);
        BeanUtils.copyProperties(lease, leaseEasyVo);
        return leaseEasyVo;
    }
}
